<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-8-25
 * Time: 下午7:07
 */
namespace app\extensions;

use Yii;
use yii\base\Exception;
use yii\base\Object;

class WechatTemplate extends Object
{
    private function send($message)
    {
        return Yii::$app->oauth->sendTemplateText($message);
    }

    /**
     * 待处理通知
     * @param $openId
     * @param $message
     * @param $content
     * @param $username
     * @param $date
     * @param $remark
     * @param string $url
     * @return mixed {{first.DATA}}
     *
     * {{first.DATA}}
     * @internal param $title
     */
    public function sendNote($openId, $message, $content, $username, $date, $remark, $url = "")
    {
//        $data = $this->array2Json(array(
//            "touser" => $openId,
//            "template_id" => "lrBtR0M6y0p4v0u0jly3dh7col3E-swspLpxqQpF_5A",/*"0liKdKacJ1vgxQ34ENfz0cqoyQMzWBCmYbF25AFNqac",*/
//            "url" => $url,
//            "topcolor" => "#3E6790",
//            "data" => array(
//                "first" => array(
//                    "value" => str_replace(['\n',"\n"],' ',$message),
//                    "color" => "#000000"
//                ),
//                "keyword1" => array(
//                    "value" => str_replace(['\n',"\n"],' ',$content),
//                    "color" => "#1E81C5"
//                ),
//                "keyword2" => array(
//                    "value" => $username,
//                    "color" => "#1E81C5"
//                ),
//                "keyword3" => array(
//                    "value" => $date,
//                    "color" => "#1E81C5"
//                ),
//                "remark" => array(
//                    "value" => str_replace(['\n',"\n"],' ',$remark),
//                    "color" => "#000000"
//                ),
//            )
//        ));
        return Yii::$app->wechat->sendTemplate($openId,'lrBtR0M6y0p4v0u0jly3dh7col3E-swspLpxqQpF_5A',$url,array(
            "first" => array(
                "value" => str_replace(['\n',"\n"],' ',$message),
                "color" => "#000000"
            ),
            "keyword1" => array(
                "value" => str_replace(['\n',"\n"],' ',$content),
                "color" => "#1E81C5"
            ),
            "keyword2" => array(
                "value" => $username,
                "color" => "#1E81C5"
            ),
            "keyword3" => array(
                "value" => $date,
                "color" => "#1E81C5"
            ),
            "remark" => array(
                "value" => str_replace(['\n',"\n"],' ',$remark),
                "color" => "#000000"
            ),
        ),'#3E6790');
    }

    /**
     * 微信专用 不转义中文
     * 将数组转换成Json格式，中文需要进行URL编码处理
     * @param $array
     * @return mixed|string
     */
    public function array2Json($array)
    {
        $this->arrayRecursive($array, 'urlencode', true);
        $json = json_encode($array);
        //替换双引号
        $json = str_replace("%22", '\"', $json);
        $json = urldecode($json);
        // ext需要不带引号的bool类型
        $json = str_replace("false", false, $json);
        $json = str_replace("true", true, $json);
        return $json;
    }

    public function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
    {
        static $recursive_counter = 0;
        if (++$recursive_counter > 1000) {
            die('possible deep recursion attack');
        }
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $this->arrayRecursive($array[$key], $function, $apply_to_keys_also);
            } else {
                $array[$key] = $function($value);
            }
            if ($apply_to_keys_also && is_string($key)) {
                $new_key = $function($key);
                if ($new_key != $key) {
                    $array[$new_key] = $array[$key];
                    unset($array[$key]);
                }
            }
        }
        $recursive_counter--;
    }
}